package com.junglone.gateway.admin.web.controller;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.junglone.gateway.admin.common.core.controller.BaseController;
import com.junglone.gateway.admin.common.core.domain.AjaxResult;
import com.junglone.gateway.admin.common.core.domain.PageDomain;
import com.junglone.gateway.admin.common.core.domain.TableDataInfo;
import com.junglone.gateway.admin.domain.MongoRoute;
import com.junglone.gateway.admin.service.MongoRouteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * Description: 网关路由操作 Controller
 *
 * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
 * @since 2021/04/04 04:50
 */
@Controller
@RequestMapping("/system/route")
public class GatewayRouteController extends BaseController {

    @Resource
    private MongoRouteService mongoRouteService;

    @GetMapping("/routes")
    public String routes() {

        return "/system/route/routes";
    }

    @PostMapping("/list")
    @ResponseBody
    public Mono<TableDataInfo> getRouteList(PageDomain pageDomain) {

        return Mono.just(super.getDataTable(mongoRouteService.getRouteList(pageDomain)));
    }

    @GetMapping("/add")
    public String add() {

        return "/system/route/add";
    }

    @RequestMapping("/addPredicate")
    public String addPredicate() {

        return "/system/route/predicate";
    }

    @RequestMapping("/addFilter")
    public String addFilter() {

        return "/system/route/filter";
    }

    @PostMapping("/addRoute")
    @ResponseBody
    public Mono<AjaxResult> addRoute(MongoRoute mongoRoute) {

        LOGGER.info("mongoRoute ==== {}", mongoRoute);

        String strId = mongoRouteService.add(mongoRoute);
        if (StringUtils.isEmpty(strId)) {
            LOGGER.error("Invoke addRoute() happened ERROR ==== mongoRoute = {}", mongoRoute);
            return Mono.just(error());
        }

        return Mono.just(success());
    }

    @RequestMapping("/edit/{strId}")
    public String edit(Model model, @PathVariable String strId) {

        MongoRoute mongoRoute = mongoRouteService.getRouteById(strId);

        model.addAttribute(mongoRoute);

        return "/system/route/edit";
    }

    @PostMapping("/editRoute")
    @ResponseBody
    public Mono<AjaxResult> editRoute(MongoRoute mongoRoute) {

        LOGGER.info("mongoRoute ==== {}", mongoRoute);

        long lCount = mongoRouteService.update(mongoRoute);
        if (0 == lCount) {
            LOGGER.error("Invoke editRoute() happened ERROR ==== mongoRoute = {}", mongoRoute);
            return Mono.just(error());
        }

        return Mono.just(success());
    }

    @PostMapping("/delete")
    @ResponseBody
    public Mono<AjaxResult> deleteRoute(MongoRoute mongoRoute) {

        if (null == mongoRoute || StringUtils.isEmpty(mongoRoute.getId().trim())) {
            LOGGER.error("routeId 不能为空!");
            return Mono.just(error("routeId 不能为空!"));
        }

        long lCount = mongoRouteService.delete(mongoRoute.getId());
        if (0 == lCount) {
            LOGGER.error("Invoke deleteRoute() happened ERROR ==== routeId = {}", mongoRoute.getRouteId());
            return Mono.just(error());
        }

        return Mono.just(success());
    }

    @RequestMapping("/detail/{strId}")
    public String detail(Model model, @PathVariable String strId) {

        MongoRoute mongoRoute = mongoRouteService.getRouteById(strId);

        model.addAttribute(mongoRoute);

        return "/system/route/detail";
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public Mono<AjaxResult> changeStatus(MongoRoute mongoRoute) {


        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);


        try {
            LOGGER.info("changeStatus ==== mongoRoute ==== {}", objectMapper.writeValueAsString(mongoRoute));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        mongoRoute.setOrder(-1);
        long lCount = mongoRouteService.update(mongoRoute);
        if (0 == lCount) {
            LOGGER.error("Invoke editRoute() happened ERROR ==== mongoRoute = {}", mongoRoute);
            return Mono.just(error());
        }

        return Mono.just(success());
    }

}
